package com.itboye.ihomebank.qianyue;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.animation.IntArrayEvaluator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.mobileim.utility.ITaskReceiver;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.QianYueAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.bean.QianYueManageBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.WoDeFangYuanBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.custom.XListView.IXListViewListener;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 签约
 * 
 */
public class OrderFragment extends BaseFragment implements Observer,
		IXListViewListener {
	TextView txt_title;
	private int pageNo = 1;
	int status;
	XListView xlistView;
	private QianYueAdapter adapter;
	public static String REFRESH = "refresh";
	HousePresenter housePresenter;
	LinearLayout noData;
	String uid = "";
	private ArrayList<QianYueManageBean> orderQueryModel = new ArrayList<>();

	public OrderFragment(int status) {
		this.status = status;
	}

	@Override
	public int initView() {
		return R.layout.activity_allorder;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		showProgressDialog("数据加载中请稍后", true);
		uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
		housePresenter = new HousePresenter(this);
		housePresenter.qianYueGuanLi(uid, status + "");
		// for (int i = 0; i < 20; i++) {
		// OrderList list = new OrderList();
		// list.setType(status);
		//
		// orderQueryModel.add(list);
		// }
		adapter = new QianYueAdapter(getActivity(), orderQueryModel,
				R.layout.item_allorder);

		xlistView.setAdapter(adapter);
		// showProgressDialog("加载中", true);
		xlistView.setXListViewListener(this);
		xlistView.setPullLoadEnable(false);
		xlistView.setPullRefreshEnable(false);
		xlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				// Intent intent = new Intent(getActivity(),
				// AllOrderDetail.class);
				// intent.putExtra("model", orderQueryModel.get(position - 1));
				// intent.putExtra("id", orderQueryModel.get(position -
				// 1).getId());
				// startActivity(intent);
			}
		});

		IntentFilter filter = new IntentFilter(REFRESH);
		getActivity().registerReceiver(receiver, filter);
		// orderPresenter = new OrderPresenter(this);
		// model = new OrderBean();
		// model.setStatus(status+"");
		// model.setUid(uid);
		//
		// model.setPageNo(pageNo + "");
		// orderPresenter.getInfo(model);
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String contraNo = intent.getStringExtra("contractNo");
			String shenfen = intent.getStringExtra("shenfen");
			Iterator<QianYueManageBean> iterator = arrayList.iterator();
			while (iterator.hasNext()) {
				QianYueManageBean bean = iterator.next();
				if (contraNo != null) {
					if (contraNo.equals(bean.getContractNo())
							&& shenfen.equals(bean.getUserType())) {
						bean.setLesserWritten("1");
						adapter.notifyDataSetChanged();
						break;
					}
				}
			}
			// model = new OrderBean();
			// model.setStatus(status+"");
			// model.setUid(uid);
			//
			// model.setPageNo(pageNo + "");
			// orderPresenter.getInfo(model);
		}
	};

	private List<QianYueManageBean> arrayList;

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (pageNo == 1) {
				orderQueryModel.clear();
			}
			arrayList = (List<QianYueManageBean>) entity.getData();
			if (arrayList != null) {
				if (arrayList.size() > 0) {
					orderQueryModel.addAll(arrayList);
					noData.setVisibility(View.GONE);
					xlistView.setVisibility(View.VISIBLE);
				} else {
					noData.setVisibility(View.VISIBLE);
					xlistView.setVisibility(View.GONE);
				}
			} else {
				noData.setVisibility(View.VISIBLE);
				xlistView.setVisibility(View.GONE);
			}

			adapter.notifyDataSetChanged();
		} else {
			noData.setVisibility(View.VISIBLE);
			xlistView.setVisibility(View.GONE);
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
		stopLoad();
	}

	// @Override
	// public void update(Observable observable, Object data) {
	// // TODO Auto-generated method stub
	// ResultEntity entity = handlerError(data);
	// if (entity != null) {
	// if(pageNo==1){
	// orderQueryModel.clear();
	// }
	// orderQueryModel.addAll(((OrderQueryModel) entity.getData()).getList());
	// adapter.notifyDataSetChanged();
	// }
	// try {
	// closeProgressDialog();
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	// stopLoad();
	// }

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageNo = 1;
		// model.setStatus(status + "");
		// model.setUid(uid);
		// model.setPageNo(pageNo + "");
		// orderPresenter.getInfo(model);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		pageNo++;
		// model.setStatus(status + "");
		// model.setUid(uid);
		// model.setPageNo(pageNo + "");
		// orderPresenter.getInfo(model);
	}

	public void stopLoad() {
		xlistView.stopLoadMore();
		xlistView.stopRefresh();
	}

}
